﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using HF.Permission.Login;
using HF.User.Data;
using HF.Framework.Log;
using HF.Framework.Windows;

namespace HF.WorkFlow.Tool
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ThreadExceptionHandler handler = new ThreadExceptionHandler();
            Application.ThreadException += new ThreadExceptionEventHandler(handler.Application_ThreadException);
            LoginDlg dlgLogin = new LoginDlg("请登录");
            dlgLogin.ShowDialog();
            if (dlgLogin.DialogResult == DialogResult.OK)
            {
                Global.UserId = dlgLogin.UserId;
                Global.UserName = UserData.GetInstance().GetUserNameById(dlgLogin.UserId);
                Global.AppPath = Application.StartupPath;
                fmFlash tmpfmFlash = new fmFlash();
                tmpfmFlash.Show();
                tmpfmFlash.BringToFront();
                Application.Run(new WorkFlowMainFrm());

            }

        }
    }
    /// 
    /// Handles a thread (unhandled) exception.
    /// 
    internal class ThreadExceptionHandler
    {
        // Handles the thread exception. 
        public void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            try
            {
                // Exit the program if the user clicks Abort.
                DialogResult result = ShowThreadExceptionDialog(e.Exception);
                if (result == DialogResult.Abort)
                    Application.Exit();
            }
            catch(Exception ex)
            {
                string errorStr="未知错误！" + ex.Message.ToString();
                try
                {
                    LogData.WriteLog(errorStr, "WfaTool");
                    WorkDialog.ErrorDlg(errorStr, "出错了");
                }
                finally
                {
                    Application.Exit();
                }
              
            }
        }

        /// 
        /// Creates and displays the error message.
        /// 
        private DialogResult ShowThreadExceptionDialog(Exception ex)
        {
            string errorMessage = "错误信息：\n\n" +
                ex.Message + "\n\n" + ex.GetType() +
                "\n\nStack Trace:\n" +
                ex.StackTrace;
            SendErrInfoFrm infoform = new SendErrInfoFrm(errorMessage);
            return infoform.ShowDialog();

        }
    }
}
